<template>
	<view class="container">
		
		<view class="section" scroll-y="true" :style="{height: wh+'px'}">
			<view class="section-item">
				<view class="item flex flex-colum space-between align-items-top">
					<!-- <uni-easyinput v-model="goodsid" placeholder="商品ID" v-if="false"/> -->
					<view class="key">
						举报原因
						</view>
					<view>
						<text class="tishi">(此举报为小程序内部渠道，对其账号做出相应处理，非微信官方渠道)</text>
						
					</view>
					<textarea name="" id="" :disable-default-padding="true" :auto-height="true" class="value" v-model="reson"  @blur="reson=reson.replace(/[^\u4E00-\u9FA5|\d|\a-zA-Z|\r\n\s,.?!，。？！…—&$=()-+/*{}[\]]/g, '')"
					 placeholder="请输入举报原因...(建议50字以内)" placeholder-style="text-align:left" maxlength="50"></textarea>
									
				</view>
				<view class="item flex flex-colum align-items-top">
					<!-- <uni-easyinput v-model="goodsid" placeholder="商品ID" v-if="false"/> -->
					<view class="key flex flex-justify-start">
						凭证(<view class="tips">为更快核实情况，请务必上传凭证。</view>)
					</view>
					<view class="value flex flex-justify-start image-box">
						<!-- <u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="6"></u-upload> -->
							<upload ref="upload" :uploadParams="{goodsid: 1111,imagevalue}"></upload>
							<!-- <image style="width: 50px;height: 50px;margin-right: 4px;margin-bottom: 8px;"
								v-for="item in fileList1" :src="item.url"></image>
							<image style="width: 50px;height: 50px;" v-if="fileList1.length < 6" @click="uploadImg()"
								class="operation-icon" src="/static/images/pai.png"></image> -->
							
					</view>
				</view>
			</view>
			
		</view>
		
		<!-- 底部按钮 start -->
		<view class="btn-box w100 fixed">
			<button class="btn btn-save" :disabled="!isclick" type="primary" @click="btnOkClick">确定</button>
		</view>
		<!-- 底部按钮 end -->

	</view>
</template>

<script>
	var self
	import upload from '@/components/upload.vue'
	const COS = require('cos-wx-sdk-v5');
	
	
	export default {
		components: {
			upload
		},
		data() {
			return {
				mediaUrl: uni.$http.mediaUrl,
				wh: 0,
				goodsid: '',
				goodsname: '',
				goodscoins: '',
				goodsdetail: '',
				fileList1: [],
				imagevalue: '',
				bei_tou: '',
				reson: '',

				userInfo: uni.getStorageSync('userInfo') || null,
				login_code: '',
				openid: '',
				access_token: '',
				checkresult: '没有经过审核',
				textcheck: false,
				imagecheck: false,
				traceidList: [],
				timer: '',
				isclick: true,
			};
		},
		onLoad(e) {
			self = this
			self.bei_tou = e.user
			this.goodsid = this.realSystime()
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 60
		},
		onShow() {
			self = this
			this.isclick = true
			// uni.login({
			// 	provider: 'weixin',
			// 	async success(res) {
			// 		self.login_code = res.code;
			// 		const postmsg = {
			// 			login_code: self.login_code
			// 		}
			// 		const {
			// 			data: ress
			// 		} = await uni.$http.post('wxGetOpenId/', postmsg)
			// 		if(ress.code==200){  
			// 			//拿到openid有何用？
			// 			self.openid = ress.data.openid
			// 			self.access_token = ress.data.access_token
			// 			// console.log('openid+access_token---',self.openid,self.access_token)
			// 		} else {
			// 			console.log(ress.msg)
			// 		}
			// 	}
			// })
		},
		methods: {
			realSystime() {
				var now = new Date(); //创建Date对象
				var year = now.getFullYear(); //获取年份
				var month = now.getMonth() + 1; //获取月份
				var date = now.getDate(); //获取日期
				var day = now.getDay(); //获取星期
				var hour = now.getHours(); //获取小时
				var minu = now.getMinutes(); //获取分钟
				var sec = now.getSeconds(); //获取秒 
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (date >= 0 && date <= 9) {
					date = "0" + date;
				}
				if (hour >= 0 && hour <= 9) {
					hour = "0" + hour;
				}
				if (minu >= 0 && minu <= 9) {
					minu = "0" + minu;
				}
				if (sec >= 0 && sec <= 9) {
					sec = "0" + sec;
				}
				var time = '' + year + month + date + hour + minu + sec;
				return time
			},
			uploadImg() {
				uni.chooseImage({
					count: 6, // 一次选择的图片数量
					sizeType: ['original', 'compressed'], // 原图或压缩图
					sourceType: ['album', 'camera'], // 图片来源
					success: (res) => {
						console.log(res.tempFiles)
						const cos = new COS({
							SecretId: 'AKIDDKRWo9cAkIvz0ptpZFl4sjUD55xdJrW7',
							SecretKey: 'd5LRK1WotmU0JiDYhePyKfp5KPL9mJhu',
						});
						res.tempFiles.forEach(file => {
							const filePath = file.path
							uni.getFileSystemManager().readFile({
								filePath: filePath,
								success: (res) => {
									cos.putObject({
										Bucket: 'lyg-1254016590', // 存储桶名称
										Region: 'ap-nanjing', // 区域
										Key: '/complain/' + Date.now() + '-' + Math
											.random().toString(36).substr(2,
											8), // 文件路径及名称
										Body: res.data,
										onProgress: (progressData) => {
											console.log('进度:', JSON.stringify(
												progressData));
										}
									}, (err, data) => {
										if (err) {
											console.error('上传失败:', err);
										} else {
											console.log('上传成功:', data);
											this.fileList1.push({
												url: 'https://' + data
													.Location
											})
										}
									});
								},
								fail: (error) => {
									console.error('读取文件失败:', error);
								}
							});
						})
					},
				})
			},
			

			// // 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				// console.log(this.fileList1)
			},
			// 新增图片
			// async afterRead(event) {
			// 	// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			// 	let lists = [].concat(event.file)
			// 	let fileListLen = this[`fileList${event.name}`].length
			// 	lists.map((item) => {
			// 		this[`fileList${event.name}`].push({
			// 			...item,
			// 			status: 'uploading',
			// 			message: '上传中'
			// 		})
			// 	})
			// 	for (let i = 0; i < lists.length; i++) {
			// 		const result = await this.uploadFilePromise(lists[i].url)
			// 		let item = this[`fileList${event.name}`][fileListLen]
			// 		this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
			// 			status: 'success',
			// 			message: '',
			// 			url: result
			// 		}))
			// 		fileListLen++
			// 	}
			// 	// console.log(this.fileList1)
			// },
			// uploadFilePromise(url) {
			// 	return new Promise((resolve, reject) => {
			// 		let a = uni.uploadFile({
			// 			url: uni.$http.baseUrl + 'fileUpload/', // 仅为示例，非真实的接口地址
			// 			filePath: url,
			// 			name: 'myfile',
			// 			formData: {
			// 				goodsid: 1111,
			// 				imagevalue: this.imagevalue,
			// 			},
			// 			success: (res) => {
			// 				var meta = JSON.parse(res.data);
			// 				// this.imagelist.push(meta.url) 
			// 				self.imagecheck = false
			// 				setTimeout(() => {
			// 					resolve(meta.data)
			// 				}, 1000)
			// 			}
			// 		});
			// 	})
			// },


			textChange() {
				// console.log('textChange')
				self.textcheck = false
			},

			async goodsPublish() {

				if (this.reson == '' || this.reson.length == 0 || this.reson == null) {
					console.log(this.reson)
					uni.showToast({
						icon: 'error',
						title: '请简要说明原因',
						duration: 1000
					});
					self.isclick = true;

				} else if (this.reson.length > 50) {
					uni.showToast({
						icon: 'error',
						title: '请简要说明原因。字数不得超过50字',
						duration: 1000
					});
					self.isclick = true;
				} else{
					let fileList = this.$refs?.upload.fileList
					// console.log(fileList)
					

					if (fileList.length == 0) {
						
						 uni.showToast({
							icon: 'error',
							title: '请务必上传图片',
							duration: 1000
						});
						self.isclick = true;
					} else {
						// var imagelists = fileList.map((item) => `/media/${ item.url.split('/media/')?.[1] }`)
						var imagelists =[]
						// console.log(imagelists)
						
						
						for (let i = 0; i < fileList.length; i++) {
							
							imagelists.push(fileList[i]['url'])
						
						// 	if (imagelists[i].indexOf('undefined') >= 0) {
						// 		console.log(imagelists[i])
						
						// 		// 证明有未上传的
						// 		self.isclick = true
						
						// 		return uni.showToast({
						// 			icon: 'error',
						// 			title: '稍等图片上传！',
						// 			duration: 2000
						// 		})
						// 	}
						}
						
						const dataPost = {
							tou_user: this.userInfo.username,
							bei_user: this.bei_tou,
							reson: this.reson,
							imagelist: imagelists
						}
						const {
							data: res
						} = await uni.$http.post('tousu/', dataPost)
						if (res.code == 200) {
							uni.showToast({
								icon: 'success',
								title: '举报成功！',
								duration: 2000
							})
							setTimeout(function() {
								uni.reLaunch({
									url: '/pages/home/home'
								})
							}, 2000);

						} else {
							uni.showToast({
								icon: 'error',
								title: '请求失败！请联系管理员' + res.msg
							})
						}
					}

				}



			},

			async btnOkClick() {
				if (self.isclick) {
					self.isclick = false;
					self.goodsPublish()
				}
			},
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f2f2f2;
		
	}
	.tishi{
		font-size: 10px;
	}
	textarea{
		min-height: 200rpx;
		text-align: left!important;
		width: 100%;
		padding: 20rpx;
		background-color: #F5F5F5;
		border-radius: 12rpx;
		box-sizing: border-box;
		
	}
	.item{
		.image-box{
			padding-left: 0;
		}
		.value{
			margin-top: 20rpx;
		}
	}
	.btn-box{
		box-shadow: 0 0 12rpx rgba(132, 129, 200, .3);
	}
	.tips{
		color: red;
	}
</style>